var setBufferParams, doBuffer;
var bufferGraphicsLayer;
var initBufferCal;

var bufferGraphics = [];
var bufferInput;

var selectedGraphicArr; // 用于存储本地加载文件的所有graphic，和selectedGraphic不同

require(["esri/geometry/Point",
    "esri/geometry/Polyline",
    "esri/geometry/Polygon",

    "esri/graphic",
    "esri/Color",

    "esri/tasks/BufferParameters",
    "esri/tasks/GeometryService",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleFillSymbol",
    "esri/symbols/SimpleLineSymbol",
    "esri/symbols/PictureMarkerSymbol",
    "esri/SpatialReference",
    "esri/layers/GraphicsLayer",
    "dojo/domReady!"
], function (Point, Polyline, Polygon, Graphic, Color, BufferParameters, GeometryService, SimpleMarkerSymbol, SimpleFillSymbol, SimpleLineSymbol, PictureMarkerSymbol, SpatialReference, GraphicsLayer) {
    var geometryService = new GeometryService(geometryServiceUrl);

    initBufferCal = function () {
        bufferGraphicsLayer = new GraphicsLayer({
            id: "bufferGraphicsLayer",
        });
        map.addLayer(bufferGraphicsLayer);
    }

    let bufferParameters = new BufferParameters();

    setBufferParams = function (graphics) {
        selectedGraphicArr = graphics;

        selectedGeometryArr = graphics.map(item => {
            return item.geometry;
        })
        //传入的参数是geometries，无法带有属性字段信息，返回值也只有图形信息
        bufferParameters.geometries = selectedGeometryArr;
        bufferParameters.unit = GeometryService.UNIT_METER;
        bufferParameters.bufferSpatialReference = shanghaiSpatialRef;
        bufferParameters.outSpatialReference = shanghaiSpatialRef;
    }

    doBuffer = function (selectedGeometryArr, bufferInput) {
        bufferGraphicsLayer.clear();
        bufferGraphics = [];
        bufferParameters.distances = [bufferInput];

        if (selectedGeometryArr[0].type == "polygon") {
            geometryService.buffer(bufferParameters, function (features) {
                var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                    new SimpleLineSymbol("solid", new Color([197, 97, 20]), 1),
                    new Color([197, 97, 20, 0.4]));

                for (var i = 0; i < features.length; i++) {
                    var graphic = new Graphic(features[i], symbol, []);
                    bufferGraphicsLayer.add(graphic);
                    bufferGraphics.push(graphic)
                }
            });
        } else if (selectedGeometryArr[0].type == "polyline") {
            geometryService.buffer(bufferParameters, function (features) {
                var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                    new SimpleLineSymbol("solid", new Color([197, 97, 20]), 1),
                    new Color([197, 97, 20, 0.4]));

                for (var i = 0; i < features.length; i++) {
                    var graphic = new Graphic(features[i], symbol, []);

                    bufferGraphicsLayer.add(graphic);
                    bufferGraphics.push(graphic)
                }
            });
        } else if (selectedGeometryArr[0].type == "point") {
            geometryService.buffer(bufferParameters, function (features) {
                var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
                    new SimpleLineSymbol("solid", new Color([197, 97, 20]), 1),
                    new Color([197, 97, 20, 0.4]));

                for (var i = 0; i < features.length; i++) {
                    var graphic = new Graphic(features[i], symbol, []);

                    bufferGraphicsLayer.add(graphic);
                    bufferGraphics.push(graphic)
                }
            });
        }
    }

    $("#doBufferBtn").click(function () {
        bufferInput = parseFloat($("#bufferInput").val());
        selectedGeometryArr = selectedGraphicArr.map(item => {
            return item.geometry;
        })
        doBuffer(selectedGeometryArr, bufferInput);
    })

    $("#saveBufferBtn").click(function () {
        if (isCtxMenu) {
            var name = `${bufferInput}米缓冲区分析结果`
            createFeatureLayer(bufferGraphics, name, "areaCoverageLayerList");
        } else {
            var name = `${localFileName}的${bufferInput}米缓冲区分析结果`
            createFeatureLayer(bufferGraphics, name, "areaCoverageLayerList");
        }
    });

    $("#bufferResetBtn").click(function () {
        bufferGraphicsLayer.clear();
        $("#bufferInput").val('');
    });

    $("#bufferInput").on("keypress", function (event) {
        if (event.keyCode == 13) {
            $("#doBufferBtn").click();
        }
    })

});